package cGUI;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;



public class Parseador extends DefaultHandler 
{

    private final XMLReader xr;
    private String texto;
    private String seleccion;
    private Ayuda a;

    public Parseador(String seleccion,Ayuda a) throws SAXException 
    {
        xr = XMLReaderFactory.createXMLReader();
        xr.setContentHandler(this);
        xr.setErrorHandler(this);
        this.seleccion=seleccion;
        this.a=a;
    }

    public void leer(final String archivoXML) throws FileNotFoundException, IOException, SAXException
    {
        FileReader fr = new FileReader(archivoXML);
        xr.parse(new InputSource(fr));
    }

    
    public void startDocument()
    {
//        System.out.println("Se comienza a parsear");
    }

    
    public void endDocument() {
//        System.out.println("Final del Documento XML");
    }

    
    public void startElement(String uri, String lName,String qName, Attributes atts)
    {
//        System.out.println("ETIQUETA(COMIENZO): " + lName);
        if (lName.equals(seleccion))
        {
	        if (atts.getLength()>0)
	        {
//		        System.out.println("->NOMBRE "+atts.getValue(0));
		        a.setNombre(atts.getValue(0));
//		        System.out.println("->TEXTO "+atts.getValue(1));
		        a.setTexto(atts.getValue(1));
//		        System.out.println(a.getTexto());
	        }
        }
	         

    }

    public void endElement(String uri, String lName,String qName)
    {
//    	System.out.println("ETIQUETA(FIN): " + lName);
//    	System.out.println("------------------------");
    }
    
    public void characters(char[] ch, int start, int length) throws SAXException
    {
        String s = String.valueOf(ch, start, length).trim();
        texto = texto + s;        
    }
}